package com.aurora.supermarket.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aurora.supermarket.R;
import com.aurora.supermarket.util.StatusBarUtils;
import com.aurora.supermarket.widget.CompatToolbar;

/**
 * Created by Administrator on 2018/3/22.
 */

public abstract class AppBaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {

    ImageView actionIvBack;
    TextView actionBarTitle;
    ImageView actionBarRightTitle;
    TextView action_bar_tv_right_title;
    CompatToolbar baseActionBar;
    private LinearLayout mBaseLayout;
    private Toast mToast;
    protected T basePresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        super.setContentView(R.layout.activity_base_action_bar);

        //沉浸式状态栏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  //4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.0
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }

        StatusBarUtils.statusBarLightMode(this);

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { //5.1
            StatusBarUtils.setStatusBarColor(this,R.color.baseColor);
        }
        basePresenter = attach();
        initViews();
    }


    /**
     * 初始化标题栏
     * @param isShowToolBar 是否显示标题栏
     * @param title 标题栏title
     * @param isShowRightTitle 是否显示右侧小图片，设置为显示需要调用initRightTitle
     */
    public void initToolbar(boolean isShowToolBar,String title,boolean isShowRightTitle){

        if(isShowToolBar){
            baseActionBar.setVisibility(View.VISIBLE);
            actionBarTitle.setText(title);
            if(isShowRightTitle){
                actionBarRightTitle.setVisibility(View.VISIBLE);
                action_bar_tv_right_title.setVisibility(View.VISIBLE);
            }else{
                actionBarRightTitle.setVisibility(View.GONE);
                action_bar_tv_right_title.setVisibility(View.GONE);
            }
        }else{
            baseActionBar.setVisibility(View.GONE);
        }

    }

    /**
     * 初始化右侧标题显示及点击事件
     * @param ImgResId 显示的图片资源id
     * @param onClickListener 点击事件
     */
    public void initRightTitle(Integer ImgResId, View.OnClickListener onClickListener){
        actionBarRightTitle.setImageResource(ImgResId);
        actionBarRightTitle.setOnClickListener(onClickListener);
    }

    /**
     * 初始化右侧标题显示及点击事件
     * @param rightTitle 显示的右标题
     * @param onClickListener 点击事件
     */
    public void initRightTitle(String rightTitle, View.OnClickListener onClickListener){
        action_bar_tv_right_title.setText(rightTitle);
        action_bar_tv_right_title.setOnClickListener(onClickListener);
    }

    private void initViews() {
        baseActionBar = findViewById(R.id.base_action_bar);
        mBaseLayout =  findViewById(R.id.abc_base_layout);
        actionBarTitle = findViewById(R.id.action_bar_title);
        actionIvBack = findViewById(R.id.action_iv_back);
        actionBarRightTitle = findViewById(R.id.action_bar_right_title);
        action_bar_tv_right_title = findViewById(R.id.action_bar_tv_right_title);
        actionIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(getLayoutInflater().inflate(layoutResID, mBaseLayout, false));
    }

    @Override
    public void setContentView(View view) {
        mBaseLayout.addView(view);
    }

    public abstract T attach();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (basePresenter != null) {
            basePresenter.cancelRequest();
            basePresenter.detachView();
        }
        if(dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
        }
    }

    public void setFullScreen() {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }


    private ProgressDialog dialog;

    public void showLoading() {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new ProgressDialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("正在加载中...");
        dialog.show();
    }

    public void showLoading(String message) {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new ProgressDialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(message);
        dialog.show();
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            try{
                dialog.dismiss();
            }catch (IllegalArgumentException e){

            }

        }
    }



    public void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    public void toggleSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    public void showLongToast(String message) {
        showToast(message, Toast.LENGTH_LONG);
    }

    public void showShortToast(String message) {
        showToast(message, Toast.LENGTH_SHORT);
    }

    private void showToast(String message, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(getApplicationContext(), message, duration);
        } else {
            mToast.setText(message);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    @Override
    public void showProcess(boolean show) {
        if(show){
            showLoading();
        }else{
            dismissLoading();
        }
    }

    @Override
    public void showMessage(String msg) {
        showShortToast(msg);
    }


    @Override
    protected void onPause() {
        super.onPause();
        hideSoftInput();
        Log.e("Base", "onDestroy: "+"关闭键盘" );
    }
}
